<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>title</title>
    <link rel="stylesheet" th:href="@{/layui/css/layui.css}" media="all">
</head>
<body>
<div class="layui-container" style="margin-top: 50px;">
    <div class="layui-container" style="width: 500px;height: 330px;padding-top: 60px;">
        <form class="layui-form" action="/user/update" method="post" id="updateForm">
            <div class="layui-form-item">
                <label class="layui-form-label">编号：</label>
                <div class="layui-inline">
                    <input type="text" name="id" th:value="${user.id}" lay-verify="required" readonly autocomplete="off" placeholder="请输入编号" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">密码：</label>
                <div class="layui-inline">
                    <input type="text" name="password" th:value="${user.password}" lay-verify="required" autocomplete="off" placeholder="请输入密码" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <button class="layui-btn" lay-submit="" lay-filter="demo2" style="margin-left: 160px;">提交</button>
            </div>
        </form>
    </div>
</div>
<script th:src="@{/layui/layui.js}" charset="utf-8"></script>
<script>
    layui.use(['form', 'element'], function(){
        var form = layui.form;
        var element = layui.element;

        form.on('submit(demo2)', function(data) {
            var user = {
                id: data.field.id,
                password: data.field.password
            };
            fetch('/user/update', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify(user)
            }).then(response => {
                if (response.ok) {
                    alert('更新成功');
                    window.location.href = '/menu/redirect/user_manage';
                } else {
                    alert('更新失败');
                }
            });
            return false; // 阻止表单默认提交
        });
    });
</script>
</body>
</html>